package com.sunday.android.fangdaifu.bukong.activity.index;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.sunday.android.fangdaifu.bukong.R;
import com.sunday.android.fangdaifu.bukong.entity.BindBack;
import com.sunday.android.fangdaifu.bukong.entity.event.FinishOption;
import com.sunday.android.fangdaifu.bukong.http.ApiClient;
import com.sunday.android.fangdaifu.bukong.utils.EditAlertView;
import com.sunday.android.fangdaifu.bukong.utils.ShowBigImage;
import com.sunday.android.fangdaifu.bukong.widgets.SelectDirectionWindow;
import com.sunday.common.event.EventBus;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.DeviceUtils;
import com.sunday.common.utils.PixUtils;
import com.sunday.common.utils.ToastUtils;
import com.sunday.member.base.BaseActivity;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.sunday.android.fangdaifu.bukong.R.id.big;
import static com.sunday.android.fangdaifu.bukong.R.id.view2;

/*
* 开始调试
* */
public class DoOptionActivity extends BaseActivity {

    @Bind(R.id.title_view)
    TextView titleView;
    @Bind(R.id.tvData)
    TextView tvData;
    @Bind(R.id.lTop)
    LinearLayout lTop;
    @Bind(R.id.tvAddress)
    TextView tvAddress;
    @Bind(R.id.tvType)
    TextView tvType;
    @Bind(R.id.tvNumber)
    TextView tvNumber;
    @Bind(R.id.tvAuto)
    TextView tvAuto;
    @Bind(R.id.image)
    ImageView image;
    @Bind(R.id.tvBtn)
    TextView tvBtn;
    @Bind(R.id.tvDirection)
    TextView tvDirection;
    @Bind(R.id.ll_we)
    LinearLayout llWe;
    @Bind(R.id.ll_sn)
    LinearLayout llSn;
    @Bind(R.id.ll_start)
    LinearLayout llStart;
    @Bind(R.id.tv_we_value)
    TextView tvWeValue;
    @Bind(R.id.tv_sn_value)
    TextView tvSnValue;


    private int w, imageW, imageH;
    private BindBack bindBack;
    private long id, plantId;
    private boolean isFinishB;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_do_option);
        ButterKnife.bind(this);
        w = DeviceUtils.getDisplay(mContext).widthPixels;
        imageW = w - PixUtils.dip2px(mContext, 26);
        imageH = 3 * imageW / 4;
        bindBack = (BindBack) getIntent().getBundleExtra("bind").getSerializable("bind");
        id = getIntent().getLongExtra("id", 0);//point id
        plantId = getIntent().getLongExtra("plantId", 0);//plant Id
        isFinishB = getIntent().getBooleanExtra("isFinish", false);//是否已调试
        initView();
    }

    private void initView() {
        titleView.setText("设备调试");
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(imageW, imageH);
        image.setLayoutParams(params);
        if (bindBack.getImageList() != null && TextUtils.isEmpty(bindBack.getImageList().get(0))) {
            Picasso.with(mContext)
                    .load(bindBack.getImageList().get(0))
                    .placeholder(R.drawable.default_error)
                    .resize(w - 60, w - 100)
                    .error(R.drawable.default_error)
                    .into(image);
        }
        tvData.setText(bindBack.getValue());
        tvAddress.setText(bindBack.getPointNo());
        tvAuto.setText(bindBack.getData());
        tvNumber.setText(bindBack.getPlantNo());
        tvDirection.setText(bindBack.getPosition());
        tvWeValue.setText(bindBack.getEw_data());
        tvSnValue.setText(bindBack.getNs_data());
        defValue=bindBack.getData();
        weValue=bindBack.getEw_data();
        snValue=bindBack.getNs_data();


        llWe.setVisibility(bindBack.getType() == 1 ? View.VISIBLE : View.GONE);
        llSn.setVisibility(bindBack.getType() == 1 ? View.VISIBLE : View.GONE);
        llStart.setVisibility(bindBack.getType() == 1 ? View.GONE : View.VISIBLE);


        if (bindBack.getType() == 1) {
            tvType.setText("倾斜测量设备");
        }
        if (bindBack.getType() == 2) {
            tvType.setText("沉降测量设备");
        }
        if (bindBack.getType() == 3) {
            tvType.setText("裂缝(位移)测量设备");
        }
        if (TextUtils.isEmpty(bindBack.getValue())) {
            lTop.setBackgroundColor(Color.parseColor("#fd7065"));
            tvBtn.setText("再次调试");
            tvData.setText("暂无数据");
            tvBtn.setClickable(true);
        } else {
            lTop.setBackgroundColor(Color.parseColor("#7cb2ff"));
            tvBtn.setText("调试成功");
            tvData.setText(bindBack.getValue());
            tvBtn.setClickable(false);
        }
    }

    @OnClick({R.id.tvNumber, R.id.tvAuto, R.id.image, R.id.tvBtn, R.id.ll_direction,R.id.ll_start,R.id.ll_we,R.id.ll_sn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.image:
                intent = new Intent(mContext, ShowBigImage.class);
                intent.putStringArrayListExtra("data", bindBack.getImageList());
                startActivity(intent);
                break;
            case R.id.tvBtn:
                if (TextUtils.isEmpty(bindBack.getNowValue())) {
                    startOption();
                } else {
                    if (bindBack.getStatus() == 0) {
                        EventBus.getDefault().post(new FinishOption());
                    }
                    finish();
                }
                break;
            case R.id.tvNumber:
                if (bindBack.getStatus() != 0 && bindBack.getStatus() != 1) {
                    return;
                }
                intent = new Intent(mContext, StartOptionActivity.class);
                intent.putExtra("id", id);
                startActivity(intent);
                break;
            case R.id.ll_start:
//                if (bindBack.getStatus()!=0 && bindBack.getStatus()!=1) {
//                    showDialog(defValue);
//                }
                showDialog(defValue, 0);//沉降或者裂缝设备
                break;
            case R.id.ll_direction:
                SelectDirectionWindow window = new SelectDirectionWindow(mContext);
                window.showPopupWindow(tvDirection);
                window.setOnSelectDirection(new SelectDirectionWindow.OnSelectDirection() {
                    @Override
                    public void selectDirection(String direction) {
                        if (!TextUtils.isEmpty(direction))
                            tvDirection.setText(direction);
                    }
                });
                break;
            case R.id.ll_we:
                showDialog(weValue, 1);//水平倾斜东西方向
                break;
            case R.id.ll_sn:
                showDialog(snValue, 2);//水平倾斜南北方向
                break;
        }
    }

    private EditAlertView alertView;

    private void showDialog(String temp, final int type) {
        alertView = new EditAlertView(mContext, "请输入设备初始值", temp, "取消", "确定", type);
        alertView.setClicklistener(new EditAlertView.ClickListenerInterface() {
            @Override
            public void doLeft() {
                alertView.dismiss();
            }

            @Override
            public void doRight(String temp) {
                //AiUpdateValue(temp);//修改初始值
                switch (type) {
                    case 0:
                        defValue = temp;
                        tvAuto.setText(defValue);
                        break;
                    case 1:
                        weValue = temp;
                        tvWeValue.setText(weValue);
                        break;
                    case 2:
                        snValue = temp;
                        tvSnValue.setText(snValue);
                        break;
                }
                alertView.dismiss();
            }
        });
        alertView.show();
    }

    String defValue = "";
    String position = "";
    String weValue = "";
    String snValue = "";

    private void startOption() {
        showLoadingDialog(0);
        //position = tvDirection.getText().toString();
        /*if (TextUtils.isEmpty(position)) {
            ToastUtils.showToast(mContext, "请选择方位");
            return;
        }*/
        Call<ResultDO<String>> call
                = ApiClient.getApiAdapter().AhTestPlant(plantId, id, position, defValue, weValue, snValue);
        call.enqueue(new Callback<ResultDO<String>>() {
            @Override
            public void onResponse(Call<ResultDO<String>> call, Response<ResultDO<String>> response) {
                dissMissDialog();
                if (response.body() == null || isFinish) {
                    return;
                }
                if (response.body().getCode() == 0) {
                    String testValue = response.body().getResult();
                    if (TextUtils.isEmpty(testValue)) {
                        lTop.setBackgroundColor(Color.parseColor("#fd7065"));
                        tvBtn.setText("再次调试");
                        tvData.setText("暂无数据");
                        tvBtn.setClickable(true);
                    } else {
                        ToastUtils.showToast(mContext, "调试完成");
                        lTop.setBackgroundColor(Color.parseColor("#7cb2ff"));
                        tvBtn.setText("调试完成");
                        tvData.setText(testValue);
                        tvBtn.setClickable(true);
                    }
                } else {
                    ToastUtils.showToast(mContext, response.body().getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResultDO<String>> call, Throwable t) {
                dissMissDialog();
                ToastUtils.showToast(mContext, "网络错误");
            }
        });
    }

    private void AiUpdateValue(final String temp) {
        showLoadingDialog(0);
        Call<ResultDO> call
                = ApiClient.getApiAdapter().AiUpdateValue(temp, id);
        call.enqueue(new Callback<ResultDO>() {
            @Override
            public void onResponse(Call<ResultDO> call, Response<ResultDO> response) {
                dissMissDialog();
                if (response.body() == null || isFinish) {
                    return;
                }
                if (response.body().getCode() == 0) {
                    ToastUtils.showToast(mContext, "初始值修改成功");
                    tvAuto.setText(temp);
                } else {
                    ToastUtils.showToast(mContext, response.body().getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResultDO> call, Throwable t) {
                dissMissDialog();
                ToastUtils.showToast(mContext, "网络错误");
            }
        });
    }
}
